﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using DAL;
using HomeStayWebCP.Models;
using HomeStayWebCP.Models.BusinessObject;

namespace HomeStayWebCP.Controllers.Admin
{
    public class ManageNewsController : Controller
    {
        public ActionResult AddNew()
        {
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                int roleID = Int32.Parse(HttpContext.Session["roleID"].ToString());
                //if admin
                if (roleID > 1)
                {
                    return View();
                }
                else
                {
                    //redirect to You doesn't have permission
                    return RedirectToAction("Login", "Admin");
                }
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }
            
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateNew(NewObject form)
        {
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                NewModel model = new NewModel();
                string regExp = "[^a-zA-Z0-9]";
                string tmp = DateTime.Now.ToString().Replace(" ", String.Empty);
                string imagename = Regex.Replace(tmp, regExp, "");

                HttpPostedFileBase file = Request.Files[0];
                if (!file.FileName.Equals(""))
                {

                    string filename = imagename + ".jpg";
                    byte[] imageSize = new byte[file.ContentLength];
                    file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
                    var path = Path.Combine(Server.MapPath("~/Content/images/NewImage/"), filename);
                    file.SaveAs(path);
                    form.Image = filename;

                }
                string createdby = HttpContext.Session["usernameAdmin"].ToString();
                model.AddNew(form,createdby);
                return RedirectToAction("ManageNews");
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }
             
        }
        public ActionResult ManageNews()
        {
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                    int roleID = Int32.Parse(HttpContext.Session["roleID"].ToString());
                    //if admin
                    if (roleID > 1)
                    {
                        NewModel newModel = new NewModel();
                        List<NewsItem> listNews = newModel.GetAllNew();
                        ViewBag.listNews = listNews;
                        return View();
                    }
                    else
                    {
                        //redirect to You doesn't have permission
                        return RedirectToAction("Login", "Admin");
                    }
                
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }
        }
        //public ActionResult ConfirmDelete(int newID, string title)
        //{
        //    ViewBag.newID = newID;
        //    ViewBag.title = title;
        //    return View();
        //}
        public ActionResult Delete(int newID)
        {
            //delete New
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                int roleID = Int32.Parse(HttpContext.Session["roleID"].ToString());
                //if admin
                if (roleID > 1)
                {
                    NewModel newModel = new NewModel();
                    var check = newModel.DeleteNew(newID);
                  
                    List<NewsItem> listNews = newModel.GetAllNew();
                    ViewBag.listNews = listNews;
                  //  return RedirectToAction("ManageNews");
                    return RedirectToAction("ManageNews");
                }
                else
                {
                    //redirect to You doesn't have permission
                    return RedirectToAction("Login", "Admin");
                }

            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }
            
        }
    }
}
